package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TShopCharge;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JComboBoxPOJO;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJRadioButtonComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TShopChargeService;
import com.ruipengkj.view.base.BaseDialog;

public class MemberShopChargeUpdate extends BaseDialog {
	private static final long serialVersionUID = 1L;
	private JButton okButton;//确定
	private JButton cancel;//取消
	private JLabelJTextFieldComponent number = null;//充值编号
	private JLabelJTextFieldComponent aMount = null;///金额
	private JLabelJTextFieldComponent giftAMount = null; //赠送金额
	private JLabelJComboBoxComponent type = null; //赠送类型
	private JLabelJTextFieldComponent operator = null; //操作人
	private JLabelJRadioButtonComponent isValid = null;//是否有效
	private JLabelJTextFieldComponent happenTimeTF = null;//操作时间
	private DataPackageComponent startTimeTF = null;//开始时间
	private DataPackageComponent endTimeTF = null;//结束时间
	private TShopChargeService tShopChargeService=BuildSpringApplication.getInstance().getTShopChargeServiceImpl();
	
	public MemberShopChargeUpdate(JDialog jd, String title, boolean b) {
		
		super(jd,title,b);
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
		//this.getElements();
	}
	private void initListener(){
		okButton.addActionListener(this);
		cancel.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}

//	public void getElements(){
//		List<TShopCharge> list = null;
//		try {
//			list = tShopChargeService.findService(null);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		if(list!=null&&list.size()!=0){
//			int length = list.size();
//			for(int i = 0; i < length; ++i) {
//				TShopCharge shopCharge=list.get(i);
//				if(shopCharge.getType().equals("1")){
//					type.addElement(new JComboBoxPOJO(shopCharge.getChargeID(),"满就送",shopCharge));
//				}else{
//					type.addElement(new JComboBoxPOJO(shopCharge.getChargeID(),"放大率",shopCharge));
//				}
//				
//			}
//		}
//	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Date date=new Date();
		TShopCharge tShopCharge = new TShopCharge();
		tShopCharge.setChargeID(number.getText().getText().trim());
		String fillMoneyT=aMount.getText().getText().trim(); //充值金额
		String giffMoney=giftAMount.getText().getText().trim();
		String operators=operator.getText().getText().trim();
		
		
		if(!MatcherFormatUtil.MatcherPriceScheme(fillMoneyT)){
			JOptionPane.showMessageDialog(null, "充值金额 不能为空，且小数点左边最多10位数字，右边最多2位数字");
			aMount.getText().setText("");
			aMount.getText().requestFocus();
			return;
		}else if(!MatcherFormatUtil.MatcherPriceScheme(giffMoney)){
			JOptionPane.showMessageDialog(null, "赠送金额 不能为空，且小数点左边最多10位数字，右边最多2位数字");
			giftAMount.getText().setText("");
			giftAMount.getText().requestFocus();
			return;
		}else if(type.getBox().getSelectedIndex() == 0){
			JOptionPane.showMessageDialog(null, "请选择类型");
			return;
		}else if(operators.equals("")||operators.length()>10){
			JOptionPane.showMessageDialog(null, "操作员不能为空，且不能大于15个字");
			return;
		}else if(startTimeTF.getText().equals("")){
			JOptionPane.showMessageDialog(null, "请选择开始时间");
			return;
		}else if(endTimeTF.getText().equals("")){
			JOptionPane.showMessageDialog(null, "请选择结束时间");
			return;
		}else{
			tShopCharge.setAMount(Double.parseDouble(fillMoneyT));
			tShopCharge.setGiftAmount(Double.parseDouble(giffMoney));
			tShopCharge.setType(type.getBox().getSelectedIndex()+"");
			//System.out.println(isValid.getSelect());
			tShopCharge.setIsValid(isValid.getSelect());
			tShopCharge.setOperator(operators);
			tShopCharge.setHappenTime(date);
			tShopCharge.setStartTime(startTimeTF.getDate());
			tShopCharge.setEndTime(endTimeTF.getDate());
			if(e.getSource()==okButton){
				//确定
				try {
					tShopChargeService.saveOrUpdateService(tShopCharge);
					//tShopChargeService.saveOrUpdateService(tShopCharge);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				//BuildSpringApplication.getInstance().getTOpLogServiceImpl().modifyService("");
				this.dispose();
			}
			if(e.getSource()==cancel){
				this.dispose();
			}
		}
		
	}
	private void initViews() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
//		JRadioButton r1 = new JRadioButton("是");
//	    JRadioButton r2 = new JRadioButton("否");
		number=new JLabelJTextFieldComponent("充值编号：",30);
		aMount = new JLabelJTextFieldComponent(" 金 额 ：", 30);
		giftAMount=new JLabelJTextFieldComponent("赠送金额：",30);
		type=new JLabelJComboBoxComponent("充值类型：",new String[]{},188);
		type.addElement(new JComboBoxPOJO("请选择……"));
		type.addElement(new JComboBoxPOJO("满就送"));
		type.addElement(new JComboBoxPOJO("放大率"));
		operator=new JLabelJTextFieldComponent("操作员：",30);
		isValid=new JLabelJRadioButtonComponent("是否有效：",60);
//		JPanel j=new JPanel();
//		ButtonGroup bg = new ButtonGroup();
//		bg.add(r1);
//		bg.add(r2);
//		isValid.add(r1);
//		isValid.add(r2);
		//happenTimeTF=new JLabelJTextFieldComponent("注册时间：",30);
		startTimeTF=new DataPackageComponent("开始时间：",30);
		endTimeTF=new DataPackageComponent("结束时间：",30);
		number.getText().setEditable(false);
		JPanel jp1=new JPanel();
		okButton=new RPJButtonBlue("确定");
		cancel=new RPJButtonGray("取消");
		jp1.add(okButton);
		jp1.add(cancel);
		jp1.setOpaque(false);
		
		main_jp.add(number);
		main_jp.add(aMount);
		main_jp.add(giftAMount);
		main_jp.add(type);
		main_jp.add(operator);
		main_jp.add(isValid);
		//main_jp.add(happenTimeTF);
		main_jp.add(startTimeTF);
		main_jp.add(endTimeTF);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	public void showDialog(){
		this.setVisible(true);
	}
	public void showDialog(TShopCharge tShopCharge){
		if(tShopCharge!=null){
			number.getText().setText(tShopCharge.getChargeID());
			aMount.getText().setText(tShopCharge.getAMount()+"");
			giftAMount.getText().setText(tShopCharge.getGiftAmount()+"");
			//System.out.print(tShopCharge.getType());
			type.getBox().setSelectedIndex(Integer.parseInt(tShopCharge.getType()));
			if(tShopCharge.isIsValid()){
				isValid.setSelect(1+"");
			}else{
				isValid.setSelect(0+"");
			}
			
			operator.getText().setText(tShopCharge.getOperator());
			startTimeTF.setDate(tShopCharge.getStartTime());
			endTimeTF.setDate(tShopCharge.getEndTime());
		}
		this.setVisible(true);
	}
}
